package com.ziyia.a29_all;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void a1(View view) {
        new AlertDialog.Builder(this)
                .setTitle("标题：年少有为")
                .setMessage("内容：还是年少有为")
                .setIcon(R.drawable.user)
                // 添加一个 "确定" 按钮
                .setPositiveButton("确定", (dialog, which) -> {
                    Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                }).create().show();
    }


    public void a2(View view) {
        String[] strings = new String[100];
        for (int i = 0; i < strings.length; i++) {
            strings[i] = "Text - " + i;
        }
        new AlertDialog.Builder(this)
                .setTitle("标题：年少有为")
                .setIcon(R.drawable.user)
                .setItems(strings, (dialog, which) -> {
                    Toast.makeText(MainActivity.this, strings[which], Toast.LENGTH_SHORT).show();
                })
                .setPositiveButton("确定", (dialog, which) -> {
                    Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                }).create().show();
    }

    public void a3(View view) {


        String[] strings = new String[10];

        for (int i = 0; i < strings.length; i++) {
            strings[i] = "Text - " + i;
        }

        new AlertDialog.Builder(this)
                .setTitle("标题：年少有为")
                .setIcon(R.drawable.user)
                // 单选对话框，下标为2的列表项为选中状态。
                .setSingleChoiceItems(strings, 2, (dialog, which) -> {
                    Toast.makeText(MainActivity.this, dialog + " - " + which, Toast.LENGTH_SHORT).show();
                })

                // 添加一个 "确定" 按钮
                .setPositiveButton("确定", (dialog, which) -> {
                    Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                }).create().show();
    }

    public void a4(View view) {
        String[] strings = new String[100];
        boolean[] booleans = new boolean[strings.length];

        for (int i = 0; i < strings.length; i++) {
            strings[i] = "Text - " + i;
            if (i < 10) booleans[i] = true;
        }

        new AlertDialog.Builder(this)
                .setTitle("标题：年少有为")
                .setIcon(R.drawable.user)
                .setMultiChoiceItems(strings, booleans, (dialog, which, isChecked) -> {
                    Toast.makeText(MainActivity.this, which + " - " + isChecked, Toast.LENGTH_SHORT).show();

                })
                // 添加一个 "确定" 按钮
                .setPositiveButton("确定", (dialog, which) -> {
                    Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                }).create().show();
    }

    public void a5(View view) {
        String[] strings = new String[100];

        for (int i = 0; i < strings.length; i++) {
            strings[i] = "Text - " + i;
        }

        new AlertDialog.Builder(this)
                .setTitle("标题：年少有为")
                .setIcon(R.drawable.user)
                .setPositiveButton("确定", (dialog, which) -> {
                    Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                })
                .setAdapter(new ArrayAdapter(this, R.layout.item, strings), (diwlog, which) -> {
                    Toast.makeText(MainActivity.this, "你选择了 = " + strings[which], Toast.LENGTH_SHORT).show();
                })
                .create().show();
    }

    public void a6(View view) {

        View inflate = getLayoutInflater().inflate(R.layout.diy_view, null);
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.user)
                .setTitle("标题：自定义View对话框")
                .setView(inflate)
                .setNegativeButton("取消", (diwlog, which) -> {
                })
                .create().show();
    }

    public void a7(View view) {
        View inflate = getLayoutInflater().inflate(R.layout.popup, null);
        PopupWindow popupWindow = new PopupWindow(inflate, 520, 520);
        popupWindow.showAsDropDown(view);

        popupWindow.showAtLocation(findViewById(R.id.linear), Gravity.CENTER, 0, 0);
        inflate.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    public void a8(View view) {
        Calendar instance = Calendar.getInstance();
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @SuppressLint("DefaultLocale")
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, String.format("%d年%d月%d日",year, month, dayOfMonth), Toast.LENGTH_SHORT).show();
            }
        },instance.get(Calendar.YEAR) , instance.get(Calendar.MONDAY) ,instance.get(Calendar.DAY_OF_MONTH) ).show();
    }

    public void a9(View view) {
        Calendar instance = Calendar.getInstance();
        Toast.makeText(this, instance.toString(), Toast.LENGTH_SHORT).show();

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, String.format("%d:%d", hourOfDay, minute), Toast.LENGTH_SHORT).show();
            }
            // 设置初始时间
        }, instance.get(Calendar.HOUR_OF_DAY), instance.get(Calendar.MINUTE), true).show();

    }

    public void a10(View view) {

//        AlertDialog dialog =new  AlertDialog.show(this, "提示", "正在登陆中…", true, false, null);

    }
}